<template>
  <div id="app">
    <el-card shadow="always">
      <el-form :inline="true" size="medium" :model="tbspecificationSearch" class="demo-form-inline">
        <el-form-item >
          <el-button type="primary" round @click="editTbspecification">新建</el-button>
        </el-form-item>

        <el-form-item >
          <el-button type="success" round>删除</el-button>
        </el-form-item>

        <el-form-item >
          <el-button type="warning" round>刷新</el-button>
        </el-form-item>

        <el-form-item label="规格名称">
          <el-input v-model="tbspecificationSearch.specName"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search" icon="el-icon-user">查询</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tbspecificationList" style="width: 100%">
        <el-table-column label="规格id" prop="id" align="center" width="180"></el-table-column>

        <el-table-column label="规格名称" prop="specName" align="center" width="180"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 15, 25, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>

    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{}}
      <el-form :model="tbspecification">
        <el-form-item label="规格id" :label-width="formLabelWidth">
          <el-input v-model="tbspecification.id" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="tbspecification.specName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveTbspecification">确 定</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import tbspecificationApi from "@/api/tbspecification"
export default {
  data() {
    return {
      tbspecificationList:[],
      page:1,
      pageSize:5,
      total:100,
      tbspecificationSearch:{},

      title:'添加规格',
      dialogFormVisible:false,
      formLabelWidth: '100px',
      tbspecification:{}
    }
  },
  created() {
    //this.findTbspecifications()
    this.findByPage(this.page,this.pageSize)
    this.search()
  },
  methods: {
    //查询所有
    async findTbspecifications() {
      const result = await tbspecificationApi.findTbspecifications()
      this.tbspecificationList = result.data
    },
    //分页查询
    async findByPage(page,pageSize){
      const {data:res} =await tbspecificationApi.findByPage(page,pageSize)
      this.tbspecificationList = res.rows
      this.total = res.total
    },
    //输入查询带分页
    async search() {
      const {data} = await tbspecificationApi.search(this.page,this.pageSize,this.tbspecificationSearch)
      this.tbspecificationList = data.rows
      this.total = data.total
    },
    //新建
    editTbspecification(){
      this.dialogFormVisible = true
    },
    //保存
    async saveTbspecification(){
      const {data:res} = await tbspecificationApi.save(this.tbspecification)
      if(res.code === 200){
        this.$message.success({
          type: 'success',
          message:res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else{
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },

    handleDelete(index, row) {
      console.log(index, row);
    },
    //每页大小改变事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    //当前页改变 事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    }
  }
}
</script>

<style>
.el-card{
  width: 80%;
}
#app{
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

</style>
